package pers.amos.edu.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.amos.edu.pojo.*;
import pers.amos.edu.service.ExaminationService;
import pers.amos.edu.service.UserService;
import pers.amos.edu.common.R;

import java.util.*;

@RestController
@RequestMapping("/edu/api")

@Api(tags = "测评接口")
public class ExaminationController {

    @Autowired
    ExaminationService examinationService;

    @Autowired
    UserService userService;

    /**
     * 获取试卷列表
     */
    @PostMapping("getExaminationPaperList")
    @ApiOperation("查看试卷列表")
    public R getExaminationPaperList() {
        List examinationPaperList = examinationService.getExaminationPaperList();
        return R.ok("获取试卷列表成功", 100).put("data", examinationPaperList);
    }

    /**
     * 获取当前试卷题目列表
     */
    @PostMapping("getExaminationQuestionsList")
    @ApiOperation("查看当前试卷题目")
    public R getExaminationQuestionsList(@RequestParam(value = "paperId") Integer paperId) {
        List questionsList = examinationService.getExaminationQuestionsList(paperId);
        if (questionsList.size() > 0) {
            List<QuestionsEntity> list = new ArrayList();
            for (int i = 0; i < questionsList.size(); i++) {
                ExaminationQuestionsEntity examinationQuestionsEntity = (ExaminationQuestionsEntity) questionsList.get(i);
                QuestionsEntity questionsEntity = new QuestionsEntity();
                questionsEntity.setId(examinationQuestionsEntity.getId());
                questionsEntity.setQuestion(examinationQuestionsEntity.getQuestion());
                List<OptionEntity> optionList = new ArrayList();
                switch (1) {
                    case 1:
                        OptionEntity optionEntity1 = new OptionEntity();
                        optionEntity1.setOptionContent(examinationQuestionsEntity.getOption1());
                        optionEntity1.setOptionNum(1);
                        optionList.add(optionEntity1);
                    case 2:
                        OptionEntity optionEntity2 = new OptionEntity();
                        optionEntity2.setOptionContent(examinationQuestionsEntity.getOption2());
                        optionEntity2.setOptionNum(2);
                        optionList.add(optionEntity2);
                    case 3:
                        if (examinationQuestionsEntity.getOption3() != null) {
                            OptionEntity optionEntity3 = new OptionEntity();
                            optionEntity3.setOptionContent(examinationQuestionsEntity.getOption3());
                            optionEntity3.setOptionNum(3);
                            optionList.add(optionEntity3);
                        } else
                            break;
                    case 4:
                        if (examinationQuestionsEntity.getOption4() != null) {
                            OptionEntity optionEntity4 = new OptionEntity();
                            optionEntity4.setOptionContent(examinationQuestionsEntity.getOption4());
                            optionEntity4.setOptionNum(4);
                            optionList.add(optionEntity4);
                        } else
                            break;
                    case 5:
                        if (examinationQuestionsEntity.getOption5() != null) {
                            OptionEntity optionEntity5 = new OptionEntity();
                            optionEntity5.setOptionContent(examinationQuestionsEntity.getOption5());
                            optionEntity5.setOptionNum(5);
                            optionList.add(optionEntity5);
                        } else
                            break;
                }
                questionsEntity.setOptionEntityList(optionList);
                list.add(questionsEntity);
            }
            return R.ok("查询题目成功", 100).put("data", list);
        }
        return R.ok("此试卷尚无题目", 100).put("data", questionsList);
    }

    /**
     * 获取测评结果
     */
    @PostMapping("getExaminationResultType")
    @ApiOperation("获取测评结果")
    public R getExaminationResultType(@RequestBody AnswerDto answerDto) {
        String openId = answerDto.getOpenId();
        String tag = answerDto.getTag();
        List<AnswerEntity> answerList = answerDto.getAnswerList();
        AnswerEntity answerEntity = answerList.get(0);
        List<ExaminationQuestionsEntity> questionsList = examinationService.getExaminationQuestionsListByQuestionId(answerEntity.getQuestionId());
        Map map = new HashMap<String, Integer>();
        String rid = "";
        int sc = 0;
        for (ExaminationQuestionsEntity examinationQuestionsEntity : questionsList) {
            for (int i = 0; i < answerList.size(); i++) {
                answerEntity = answerList.get(i);
                if (examinationQuestionsEntity.getId() == answerEntity.getQuestionId()) {
                    if (answerEntity.getAnswer() == 0) {
                        break;
                    } else if (answerEntity.getAnswer() == 1) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption1ResultTypeId());
                        sc = examinationQuestionsEntity.getOption1Score();
                    } else if (answerEntity.getAnswer() == 2) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption2ResultTypeId());
                        sc = examinationQuestionsEntity.getOption2Score();
                    } else if (answerEntity.getAnswer() == 3) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption3ResultTypeId());
                        sc = examinationQuestionsEntity.getOption3Score();
                    } else if (answerEntity.getAnswer() == 4) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption4ResultTypeId());
                        sc = examinationQuestionsEntity.getOption4Score();
                    } else if (answerEntity.getAnswer() == 5) {
                        rid = String.valueOf(examinationQuestionsEntity.getOption5ResultTypeId());
                        sc = examinationQuestionsEntity.getOption5Score();
                    }

                    if (map.containsKey(rid)) {
                        sc = (Integer) map.get(rid) + sc;
                        map.put(rid, sc);
                    } else {
                        map.put(rid, sc);
                    }
                    break;
                }
            }
        }
        rid = "";
        sc = 0;
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            if (entry.getValue() > sc) {
                rid = entry.getKey();
                sc = entry.getValue();
            } else if (entry.getValue().equals(sc) && sc != 0) {
                rid = rid + "," + entry.getKey();
            }
        }
        List resultTypeIdList = new ArrayList();
        if (rid.contains(",")) {
            String[] tmp = rid.split(",");
            for (String s : tmp) {
                resultTypeIdList.add(s);
            }
        } else {
            resultTypeIdList.add(rid);
        }
        List resultList = examinationService.getExaminationResultTypeList(resultTypeIdList);


        ExaminationResultTypeEntity erte = (ExaminationResultTypeEntity) resultList.get(0);
        ExaminationPaperEntity epe = examinationService.getExaminationPaperById(erte.getPaperId());

        if (tag.equals("share")){
            //通过分享测评
            ShareExminationPaperEntity entity =  new ShareExminationPaperEntity();
            entity.setId(null);
            entity.setPaperId(erte.getPaperId()+"");
            entity.setOpenId(answerDto.getOpenId());
            entity.setShareOpenId(answerDto.getShareOpenId());
            entity.setResult(rid);
            entity.setTestUserName(answerDto.getTestUserName());
            entity.setTestUserMobile(answerDto.getTestUserPhone());
            entity.setTestUserAchievement(answerDto.getTestUserAchievement());
            entity.setTestUserSchool(answerDto.getTestUserSchool());
            entity.setHeadUrl(answerDto.getHeadUrl());

            examinationService.insertExaminationResultByShare(entity);
        }else {
            //自己测评
            ExaminationResultEntity ere = new ExaminationResultEntity();
            ere.setId(null);
            ere.setPaperId(epe.getId());
            ere.setPaperName(epe.getName());
            ere.setPaperCoverPath(epe.getCoverPath());
            ere.setPaperDescription(epe.getDescription());

            ere.setOpenId(openId);
            ere.setResultTypeId(rid);

            examinationService.insertExaminationResult(ere);
        }




        return R.ok("查询结果成功", 100).put("data", resultList);
    }

    /**
     * 获取测评记录列表
     */
    @PostMapping("getExaminationResultList")
    @ApiOperation("查看当前用户测评记录, 需要传入openId")
    public R getExaminationResultList(String openId) {
        List resultList = examinationService.getExaminationResultListByUserId(openId);
        if (resultList.size() > 0) {
            return R.ok("查询测评记录成功", 100).put("data", resultList);
        } else {
            return R.ok("您尚未做过测评试卷", 100);
        }
    }

    /**
     * 获取测评记录详情
     */
    @PostMapping("getExaminationResultDetail")
    @ApiOperation("获取测评记录详情")
    public R getExaminationResultDetail(@RequestBody Map map) {
        int resultTypeId = Integer.valueOf(map.get("resultTypeId").toString());
        ExaminationResultTypeEntity erte = examinationService.getExaminationResultTypeById(resultTypeId);
        if (erte != null) {
            return R.ok("查询题目成功", 100).put("data", erte);
        } else {
            return R.ok("此试卷尚无题目", 100);
        }
    }

    /**
     * 获取我分享的测评结果
     */
    @PostMapping("getMyExaminationResultList")
    @ApiOperation("获取我分享的测评结果")
    public R getMyShareExaminationResultList(@RequestBody Map map) {
        String openId = (String) map.get("openId");
        List<ShareExminationPaperEntity> data = examinationService.getMyExaminationPaper(openId);

        if (data != null) {
            return R.ok("获取分享测评结果成功", 100).put("data", data);
        } else {
            return R.ok("获取分享测评结果失败", 100);
        }
    }

    /**
     * 获取我分享的测评结果详情
     */
    @PostMapping("getShareExaminationResultByTypes")
    @ApiOperation("获取我分享的测评结果详情")
    public R getShareExaminationResultByTypes(@RequestBody Map map) {
        String rid = (String) map.get("ids");
        List resultTypeIdList = new ArrayList();
        if (rid.contains(",")) {
            String[] tmp = rid.split(",");
            for (String s : tmp) {
                resultTypeIdList.add(s);
            }
        } else {
            resultTypeIdList.add(rid);
        }
        List resultList = examinationService.getExaminationResultTypeList(resultTypeIdList);


        if (resultList != null) {
            return R.ok("获取分享测评结果成功", 100).put("data", resultList);
        } else {
            return R.ok("获取分享测评结果失败", 100);
        }
    }


}
